package com.foreveross.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HrBusinessService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HrBusiness;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/SysHrBusinessController")
public class SysHrBusinessController{

	private Logger log = Logger.getLogger(SysHrBusinessController.class);

	@Resource
	HrBusinessService hrBusinessService;

	@Resource
	private LogService logService;
	
	/**
	 * 获取商户管理LIST
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrBusinessList")
	public void getHrBusinessList(HttpServletRequest request,
			HttpServletResponse response,String userId,String name,String houseId,
			String page,String rows) {
		ResultVo resultVo = new ResultVo();
		try {
			
			int total = (int)hrBusinessService.getHrBusinessListCount(userId,name, houseId);
			PageVo<HrBusiness> pageVo = new PageVo<HrBusiness>(page,total,rows);
			List<HrBusiness> list = hrBusinessService.getHrBusinessList(userId,name, houseId,pageVo.getpageIndex(),pageVo.getpageSize());
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("商户管理 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("商户管理 查看失败");
			log.error("商户管理 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看商户管理详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrBusinessById")
	public void getHrBusinessById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrBusiness hrBusiness = hrBusinessService.getHrBusinessById(id);
			resultVo.setSuccess(true);
			resultVo.setData(hrBusiness);
			log.info("商户管理 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("商户管理  查看失败");
			log.error("商户管理 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getBusinessSelect")
	public void getBusinessSelect(HttpServletRequest request,
			HttpServletResponse response,String userId,String houseId,String id) {
		List<HrBusiness> hrBusinessList = null;
		ResultVo resultVo = new ResultVo();
		try {
			hrBusinessList = hrBusinessService.getBusinessSelect(userId,houseId, id, 0, Constants.PAGE_MAX_SIZE);
			resultVo.setData(hrBusinessList);
			resultVo.setSuccess(true);
			log.info("商户管理  查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			log.error("商户管理 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 添加更新商户管理
	 * 
	 * @param request
	 * @param response
	 * @param HrBusiness
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHrBusiness")
	public void addOrUpdateHrBusiness(HttpServletRequest request,
			HttpServletResponse response,String userId, HrBusiness hrBusiness,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			if(Constants.UPDATE_ADD.equals(updateType)){
				hrBusiness.setCreateId(new SysUser(userId));
				hrBusiness.setCreateTime(new Date());
			}
			hrBusinessService.saveOrUpdate(hrBusiness, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(
					CommonUtil.getAddrIp(request),
					Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"商户管理" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "商户管理["
							+ hrBusiness.toString() + "]");
			log.info("商户管理 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("商户管理 " + updateType + "失败");
			log.error("商户管理" + updateType + "失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	

	/**
	 * 删除商户管理
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHrBusiness")
	public void deleteHrBusiness(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			hrBusinessService.deleteObjecList("HrBusiness", ids);
			resultVo.setSuccess(true);
			logService.saveLog(CommonUtil.getAddrIp(request),
					Constants.OPERATE_TYPE_BACKGROUND, Constants.LOG_DEL,
					"删除商户管理", AuthUtil.getUserid(request), "商户管理ID[" + ids + "]");
			log.info("商户管理删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage(Constants.SYS_BASEDATA_DEL_ERRE);
			log.error("商户管理删除失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
